package com.ucode.application.controller.sys;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ucode.application.form.sys.RoleForm;
import com.ucode.sys.service.RoleService;
import com.ucode.sys.vo.RoleVo;
import com.ucode.tool.base.ResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="角色管理")
@RestController
@RequestMapping("/sys-role")
@Validated
public class RoleController {

    @Autowired
    private RoleService roleService;
    
    @PreAuthorize("hasAuthority('sys:role:read') or hasRole('ADMIN')")
    @ApiOperation("根据条件分页查询列表")
    @GetMapping(value = "/list")
    public ResponseResult<Object> findPage(
            @ApiParam(value ="日期")  @RequestParam(value = "ss") Date ss,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        
        
        return ResponseResult.success();
    }
    
    @PreAuthorize("hasAuthority('sys:role:read') or hasRole('ADMIN')")
    @ApiOperation(value="根据主键查询")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getById/{id}")
    public ResponseResult<Object> getById(@PathVariable(name="id",required=true) Long id){
//        Menu menu = menuService.findById(id);
        return ResponseResult.success();
    }
    
    @PreAuthorize("hasAuthority('sys:role:write') or hasRole('ADMIN')")
    @ApiOperation(value="创建")
    @PostMapping("/create")
    public ResponseResult<Object> create(@Validated @RequestBody RoleForm form) {
        
        RoleVo roleVo = new RoleVo();
        roleVo.setName(form.getName());
        roleVo.setRemark(form.getRemark());
        roleVo.setStatuz(form.getStatuz());
        roleVo.setMenuIds(form.getMenuIds());
        roleVo = roleService.insert(roleVo);
        
        return roleVo != null ? ResponseResult.success(roleVo) : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:role:write') or hasRole('ADMIN')")
    @ApiOperation(value="更新")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/update/{id}")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
            @Validated @RequestBody RoleForm form) {
        
        RoleVo roleVo = new RoleVo();
        roleVo.setName(form.getName());
        roleVo.setRemark(form.getRemark());
        roleVo.setStatuz(form.getStatuz());
        roleVo.setMenuIds(form.getMenuIds());
        roleVo.setId(id);
        int i = roleService.update(roleVo);
        
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
}
